//
//  OrderDetailInfoCell.swift
//  Box
//
//  Created by mn on 2025/4/1.
//

import UIKit
import SnapKit

class OrderDetailInfoCell: UITableViewCell {

    let stackView = UIStackView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)
        contentView.backgroundColor = .init(hex: "#F4F6F8")

        let bgView = UIView()
        bgView.backgroundColor = .white
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 16, left: 16, bottom: 0, right: 16))
        }
        bgView.layer.cornerRadius = 19
        bgView.addSubview(stackView)
        
        stackView.axis = .vertical
        stackView.spacing = 12
        stackView.distribution = .fillEqually
        stackView.backgroundColor = .white
        stackView.layer.cornerRadius = 19

        stackView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0))
        }

        let titles = ["订单编号", "创建时间", "订单总价", "订单时间", "快递编号"]
        let values = ["2024052103085516", "2022/05/21   13:11", "￥7499.00", "2022/05/21   13:11", "23756652324"]

        for (title, value) in zip(titles, values) {
            let infoView = InfoView()
            infoView.configure(title: title, value: value)
            stackView.addArrangedSubview(infoView)
        }
    }
    
    func configure(with model: OrderDetailModel?) {
        guard let model else { return }
        let price = String(format: "￥%.2f", Float(model.price) * 0.01)
        let date = model.createTime.YMDHMS()
        let values = [model.postNo,
                      date,
                      price,
                      model.updateTime?.YMDHMS() ?? date,
                      model.postNo]
        
        for (view, value) in zip(stackView.arrangedSubviews, values) {
            if let info = view as? InfoView{
                info.valueLabel.text = value
            }
        }
    }
}

class InfoView: UIView {
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        label.textColor = UIColor(hex: "#333333")
        return label
    }()
    
    let valueLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = UIColor(hex: "#767E88")
        label.textAlignment = .right
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(titleLabel)
        addSubview(valueLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.top.bottom.equalToSuperview().inset(3)
        }
        
        valueLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }
    
    func configure(title: String, value: String) {
        titleLabel.text = title
        valueLabel.text = value
    }
}
